<?php

namespace App\Models;

use Illuminate\Support\Facades\View;

/**
 * 主题处理类
 * @package App\Models
 */
class Theme {
    private static $css;
    private static $js;
    private static $_instance;

    public static function init() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 添加css样式
     * @param string $cssPath css样式路径
     * @param int $weight 权重，数字越大越靠后
     */
    public static function addCss($cssPath, $weight = 0) {
        if (!$cssPath) {
            return false;
        }
        $currentCss = '<link rel="stylesheet" href="' . $cssPath . '">';
        $css = self::$css;
        $css[$weight][] = $currentCss;
        self::$css = $css;
    }

    /**
     * 获取css样式的html
     * @return string
     */
    public static function getCss() {
        //处理为完全正序
        $cssHtml = '';
        if (self::$css) {
            foreach (self::$css as $css) {
                foreach ($css as $row) {
                    $cssHtml .= $row;
                }
            }
        }

        return $cssHtml;
    }

    /**
     * 添加js样式
     * @param string $jsPath js样式路径
     * @param int $weight 权重，数字越大越靠后
     */
    public static function addJs($jsPath, $weight = 0) {
        if (!$jsPath) {
            return false;
        }
        $currentJs = '<script src="' . $jsPath . '"></script>';
        $js = self::$js;
        $js[$weight][] = $currentJs;
        self::$js = $js;
    }

    /**
     * 获取js样式的html
     * @return string
     */
    public static function getJs() {
        //处理为完全正序
        $jsHtml = '';
        if (self::$js) {
            foreach (self::$js as $js) {
                foreach ($js as $row) {
                    $jsHtml .= $row;
                }
            }
        }
        return $jsHtml;
    }

    public static function output() {
        View::share("css", self::getCss());
        View::share("js", self::getJs());
    }
}